//-----------------------------------------------------------------------
// <copyright file="a_CatSubDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CatSubDAOBase
    {
        #region Common methods
        public virtual a_CatSub Createa_CatSubFromReader(IDataReader reader)
        {
            a_CatSub item = new a_CatSub();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SubCatID"))) item.SubCatID = (int)reader["SubCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("MainCatID"))) item.MainCatID = (int)reader["MainCatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UName"))) item.UName = (string)reader["UName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatPopularity"))) item.StatPopularity = (int)reader["StatPopularity"];
                if (!reader.IsDBNull(reader.GetOrdinal("Ordering"))) item.Ordering = (int)reader["Ordering"];
                if (!reader.IsDBNull(reader.GetOrdinal("Introduction"))) item.Introduction = (string)reader["Introduction"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_CatSub methods
            
        public virtual void Createa_CatSub(a_CatSub a_CatSub)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_ins");
                
                database.AddInParameter(dbCommand, "@MainCatID", DbType.Int32, a_CatSub.MainCatID);
                database.AddInParameter(dbCommand, "@UName", DbType.String, a_CatSub.UName);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_CatSub.Name);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_CatSub.StatPopularity);
                database.AddInParameter(dbCommand, "@Ordering", DbType.Int32, a_CatSub.Ordering);
                database.AddInParameter(dbCommand, "@Introduction", DbType.String, a_CatSub.Introduction);
                database.AddOutParameter(dbCommand, "@SubCatID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_CatSub.SubCatID = (int)database.GetParameterValue(dbCommand, "@SubCatID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_CatSub methods
        
        public virtual void Updatea_CatSub(a_CatSub a_CatSub)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_upd");            
                
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, a_CatSub.SubCatID);
                database.AddInParameter(dbCommand, "@MainCatID", DbType.Int32, a_CatSub.MainCatID);
                database.AddInParameter(dbCommand, "@UName", DbType.String, a_CatSub.UName);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_CatSub.Name);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_CatSub.StatPopularity);
                database.AddInParameter(dbCommand, "@Ordering", DbType.Int32, a_CatSub.Ordering);
                database.AddInParameter(dbCommand, "@Introduction", DbType.String, a_CatSub.Introduction);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_CatSub methods
        public virtual void Deletea_CatSub(int subCatID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_del");
                
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, subCatID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_CatSub methods
        
        public virtual a_CatSub Geta_CatSub(int subCatID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_sel");
                
                database.AddInParameter(dbCommand, "@SubCatID", DbType.Int32, subCatID);
                
                a_CatSub a_CatSub =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatSub = Createa_CatSubFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatSub;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CatSubList methods
        public virtual a_CatSubCollection Geta_CatSubList(a_CatSubColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CatSubCollection a_CatSubCollection = new a_CatSubCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatSub a_CatSub = Createa_CatSubFromReader(reader);
                        a_CatSubCollection.Add(a_CatSub);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatSubCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CatSubCollection Geta_CatSubList(a_CatSubColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CatSubList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        public virtual a_CatSub Geta_CatSubByName(string name)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_selByName");

                database.AddInParameter(dbCommand, "@Name", DbType.String, name);

                a_CatSub a_CatSub = null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatSub = Createa_CatSubFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatSub;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CatSubCollection Geta_CatSubListByCateMain(int CatMainId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_selListByCatMain");

                database.AddInParameter(dbCommand, "@CatMainId", DbType.Int32, CatMainId);

                a_CatSubCollection a_CatSubCollection = new a_CatSubCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatSub a_CatSub = Createa_CatSubFromReader(reader);
                        a_CatSubCollection.Add(a_CatSub);
                    }
                    reader.Close();
                }
                return a_CatSubCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region Geta_CatSubList_mcid methods
        public virtual a_CatSubCollection Geta_CatSubList_mcid(Int32 mcid, a_CatSubColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_selList_mcid");

                database.AddInParameter(dbCommand, "@Mcid", DbType.Int32, mcid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_CatSubCollection a_CatSubCollection = new a_CatSubCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatSub a_CatSub = Createa_CatSubFromReader(reader);
                        a_CatSubCollection.Add(a_CatSub);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatSubCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_CatSubCollection Geta_CatSubList_mcid(Int32 mcid, a_CatSubColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_CatSubList_mcid(mcid, orderBy, orderDirection, 0, 0, out totalRecords);
        }


        #endregion

        public virtual a_CatSubCollection Geta_CatSubList_join_cattree(Int32 mcid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatSub_selList_join_cattree");

                database.AddInParameter(dbCommand, "@CatMainId", DbType.Int32, mcid);
                a_CatSubCollection a_CatSubCollection = new a_CatSubCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatSub a_CatSub = Createa_CatSubFromReader(reader);
                        a_CatSubCollection.Add(a_CatSub);
                    }
                    reader.Close();
                }
                return a_CatSubCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}